package com.lottery.ruok.lottrey.adapter.fragmentManager;

import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.fragment.DistributionFragment;
import com.lottery.ruok.lottrey.fragment.LongDragonFragment;
import com.lottery.ruok.lottrey.fragment.PrizeNumberFragment;
import com.lottery.ruok.lottrey.fragment.TwoSidedStatisticsFragment;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PrizeListPagerFragmentManager {
    public static final int TYPE_PRIZE_NUMBER = 0;//
    public static final int TYPE_PRIZE_TWO_SIDE = 1;
    public static final int TYPE_PRIZE_LONGDRAGON = 2;
    public static final int TYPE_PRIZE_DISTRIBUT = 3;

    private static HashMap<Integer, BaseFragment> map = new HashMap<>();
    private static LotteryStatOneResponse data;
    private static String[] titles;

    public static BaseFragment getFragment(int key) {
        if (map.containsKey(key)) {
            return map.get(key);
        }
        BaseFragment fragment = null;
        switch (key) {
            case TYPE_PRIZE_NUMBER:
                fragment = new PrizeNumberFragment();
                PrizeNumberFragment prizeNumberFragment = (PrizeNumberFragment) fragment;
                prizeNumberFragment.setData(data);
                break;
            case TYPE_PRIZE_TWO_SIDE:
                fragment = new TwoSidedStatisticsFragment();
                TwoSidedStatisticsFragment twoSidedStatisticsFragment = (TwoSidedStatisticsFragment) fragment;
                twoSidedStatisticsFragment.setData(data);
                break;
            case TYPE_PRIZE_LONGDRAGON:
                fragment = new LongDragonFragment();
                LongDragonFragment longDragonFragment = (LongDragonFragment) fragment;
                longDragonFragment.setData(data);

                break;
            case TYPE_PRIZE_DISTRIBUT:
                fragment = new DistributionFragment();
                DistributionFragment distributionFragment = (DistributionFragment) fragment;
                distributionFragment.setData(data);
                break;
        }
        map.put(key, fragment);
        return fragment;
    }

    public static int getSize() {
        return titles.length;
    }

    public static String getTitle(int position) {
        if (titles != null && titles.length > 0) {
            return titles[position];
        } else {
            return "";
        }
    }

    public static void setData(LotteryStatOneResponse source) {
        if (source!=null&&source.getOnetab()!=null) {
            data = source;
            titles = data.getOnetab().split(",");
            setItemData();
        }
    }

    public static void setItemData() {
        Iterator<Map.Entry<Integer, BaseFragment>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<Integer, BaseFragment> entry = entries.next();
            switch (entry.getKey()) {
                case TYPE_PRIZE_NUMBER:
                    PrizeNumberFragment prizeNumberFragment = (PrizeNumberFragment) map.get(entry.getKey());
                    prizeNumberFragment.setData(data);
                    break;
                case TYPE_PRIZE_TWO_SIDE:
                    TwoSidedStatisticsFragment twoSidedStatisticsFragment = (TwoSidedStatisticsFragment) map.get(entry.getKey());
                    twoSidedStatisticsFragment.setData(data);
                    break;
                case TYPE_PRIZE_LONGDRAGON:
                    LongDragonFragment longDragonFragment = (LongDragonFragment) map.get(entry.getKey());
                    longDragonFragment.setData(data);
                    break;
                case TYPE_PRIZE_DISTRIBUT:
                    DistributionFragment distributionFragment = (DistributionFragment) map.get(entry.getKey());
                    distributionFragment.setData(data);
                    break;
            }
        }
    }

    public static void clear() {
        map.clear();
    }
}
